<template>
  <div class="singer-container"
       :style='{bottom: contentBottom + "px"}'>
    <singer-list :data='list'
                 class="singer-content"
                 ref='singer'
                 @select='todetail'></singer-list>
    <div class="loading-container"
         v-show="!list.length">
      <loading></loading>
    </div>
    <transition name='slide'
                mode='out-in'>
      <router-view></router-view>
    </transition>
  </div>
</template>

<script>
import { getSingerList2 } from 'api/singer.js'
import { ERR_OK } from 'api/config.js'
import SingerInit from 'common/js/singer.js'
import SingerList from 'base/singerList/singerList'
import Loading from 'base/loading/loading'
import { SET_SINGER } from 'store/mutation_types'
import { mapMutations } from 'vuex'
import { playMiniMixin } from 'common/js/mixin'
const HOT_NAME = '热门'
const HOT_LENGTH = 10

export default {
  mixins: [playMiniMixin],
  data () {
    return {
      // 数据
      list: []
    }
  },
  methods: {
    // 去详情
    todetail (item) {
      this.$router.push({
        // 需要去router命名
        name: 'SingerDetail',
        params: {
          id: item.id
        }
      })
      this.setSinger(item)
    },
    async _getSingerList () {
      let res = await getSingerList2()
      if (res.code === ERR_OK) {
        // console.log(res.data.list)
        // 数据处理 [ {title:"热门",items: []}, [title:"A": ,items: []]]
        this.list = this._handleSinger(res.data.list)
        setTimeout(() => {
          this.$refs.singer.refresh()
        }, 20)
      }
    },
    // 处理歌手数据
    _handleSinger (data) {
      let hot = {
        title: HOT_NAME,
        items: []
      }
      let map = []
      data.forEach((item, index) => {
        if (index < HOT_LENGTH) {
          hot.items.push(new SingerInit({
            name: item.Fsinger_name,
            id: item.Fsinger_mid
          }))
        }
        let key = item.Findex
        if (!map[key]) {
          map[key] = {
            title: key,
            items: []
          }
        }
        map[key].items.push(new SingerInit({
          name: item.Fsinger_name,
          id: item.Fsinger_mid
        }))
      })
      // 有不是 a-z的
      let az = []
      // 其他为#
      let other = []
      for (const key in map) {
        if (map.hasOwnProperty(key)) {
          const val = map[key]
          if (val.title.match(/[a-zA-Z]/)) {
            az.push(val)
          } else {
            val.title = '#'
            other.push(val)
          }
        }
      }
      // 字母排序
      az.sort((a, b) => {
        return a.title.charCodeAt(0) - b.title.charCodeAt(0)
      })
      hot = [hot, ...az, ...other]
      // console.log(hot)
      return hot
    },
    ...mapMutations({
      setSinger: SET_SINGER
    }),
    refresh () {
      this.$refs.singer.refresh()
    }
  },
  created () {
    this._getSingerList()
  },
  mounted () {

  },
  components: {
    SingerList,
    Loading
  }
}
</script>

<style scoped lang='stylus'  rel='stylesheet/stylus'>
// better-scroll的父元素的固定高度
.singer-container
  position fixed
  top 88px
  bottom 0
  left 0
  width 100%
  z-index 1
.singer-content
  height 100%
  overflow hidden
</style>